package com.vdong.restful.service.impl;

import com.vdong.data.model.Shipper;
import com.vdong.data.model.ShipperApplication;
import com.vdong.data.model.enums.State;
import com.vdong.data.service.ShipperApplicationService;
import com.vdong.data.service.ShipperService;
import com.vdong.restful.api.response.AllTracesRequest;
import com.vdong.restful.api.response.LogisticFormRequest;
import com.vdong.restful.api.response.Response;
import com.vdong.restful.service.ShipperFunctionService;
import com.vdong.restful.staticVariable.GlobalStaticVariable;
import com.vdong.supplier.request.supplierbird.EbusinessOrderHandleRequest;
import com.vdong.supplier.request.supplierbird.EbusinessOrderHandleRequestDataRequest;
import com.vdong.supplier.response.supplierbird.EbusinessOrderHandleResponse;
import com.vdong.supplier.response.supplierbird.KdnResponse;
import com.vdong.supplier.response.supplierbird.Traces;
import com.vdong.supplier.service.supplierbird.PostQueryClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShipperFunctionServiceImpl implements ShipperFunctionService {

    @Autowired
    private PostQueryClassService postQueryClassService;
    @Autowired
    private ShipperService shipperService;
    @Autowired
    private ShipperApplicationService shipperApplicationService;
    @Value("${kuaidn.requestType}")
    private String requestType;

    @Value("${kuaidn.applicationId}")
    private int applicationId;

    @Override
    public Response trajectoryQuery(String logisticsNumber, int shipperId) {
        ShipperApplication shipperApplication = shipperApplicationService.get(applicationId);
        Shipper shipper = shipperService.findById(shipperId);

        if (shipper != null) {
            if (shipper.getTrajectoryQuery() == State.OFF) {
                return Response.failMessage(GlobalStaticVariable.NO, "该快递公司不支持该接口功能");
            }

            EbusinessOrderHandleRequestDataRequest ebusinessOrderHandleRequestDataRequest = new EbusinessOrderHandleRequestDataRequest(
                    "", shipper.getCode(), logisticsNumber);
            EbusinessOrderHandleRequest ebusinessOrderHandleRequest = new EbusinessOrderHandleRequest(
                    ebusinessOrderHandleRequestDataRequest, shipperApplication.getAppid(), requestType,
                    shipperApplication.getAppkey(), "2");
            KdnResponse a = postQueryClassService.EbusinessOrderHandle(ebusinessOrderHandleRequest);

            if (a.getSuccess()) {
                EbusinessOrderHandleResponse e = (EbusinessOrderHandleResponse) a.getResponseParent();
                LogisticFormRequest logisticFormRequest = new LogisticFormRequest();

                logisticFormRequest.setId(e.getLogisticCode() + shipperId);
                List<AllTracesRequest> lsalTracesRequest = new ArrayList<>();

                for (Traces traces : e.getTraces()) {
                    AllTracesRequest alTracesRequest = new AllTracesRequest();
                    alTracesRequest.setAcceptStation(traces.getAcceptStation());
                    alTracesRequest.setAcceptTime(traces.getAcceptTime());

                    lsalTracesRequest.add(alTracesRequest);
                }

                logisticFormRequest.setLogisticCode(e.getLogisticCode());
                logisticFormRequest.setShipperId(shipperId);
                logisticFormRequest.setShipperName(shipper.getName());
                logisticFormRequest.setState(e.getState());
                logisticFormRequest.setTraces(lsalTracesRequest);
                return Response.successBean(logisticFormRequest);

            } else if (!a.getSuccess()) {
                return Response.failMessage(GlobalStaticVariable.EXCEPTION, a.getMessage());
            } else {
                return Response.failMessage(GlobalStaticVariable.EXCEPTION, "网络异常或不支持该业务查询");
            }

        } else

        {

            return Response.failMessage(GlobalStaticVariable.EXCEPTION, "无该 shipperId ");

        }
    }

}
